package com.anjie.powerproject.controller;

import com.anjie.powerproject.common.Result;
import com.anjie.powerproject.pojo.dto.DynamicPropertyDTO;
import com.anjie.powerproject.pojo.dto.DynamicPropertyModifyCommandDTO;
import com.anjie.powerproject.service.DynamicPropertyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "dynamic/property")
public class DynamicPropertyController {
    private static final Logger logger = LoggerFactory.getLogger(DynamicPropertyController.class);

    @Autowired
    DynamicPropertyService dynamicPropertyService;

    /**
     * 获取某个图谱下的所有工况策略（含动态调节条件）
     */
    @RequestMapping(value = "getall")
    Result<List<DynamicPropertyDTO>> getall(@RequestParam Integer graphId) {
        try {
            return Result.ok(dynamicPropertyService.getall(graphId));
        } catch (Exception ex) {
            logger.error("查询动态调节列表失败 errmsg={}", ex.getMessage(), ex);
            return Result.error(String.format("查询动态调节列表失败 [%s]", ex.getMessage()));
        }
    }

    /**
     * 修改某个图谱下的某个工况的动态调节条件
     */
    @RequestMapping(value = "modify")
    @ResponseBody
    Result<Void> modify(@RequestBody DynamicPropertyModifyCommandDTO command) {
//        System.out.println(command.toString());
//        for (DynamicPropertyDTO property : command.getProperties()) {
//            System.out.println(property.toString());
//        }
        try {
//            System.out.println(command.toString());
            dynamicPropertyService.modify(command);
            return Result.ok(null);
        } catch (Exception ex) {
            logger.error("修改动态调节列表失败 errmsg={}", ex.getMessage(), ex);
            return Result.error(String.format("修改动态调节列表失败 [%s]", ex.getMessage()));
        }
    }

    /**
     * 提供给python使用计算当前工况动态调节调节执行结果
     */
    @RequestMapping(value = "execute")
    Result<Boolean> excute(@RequestParam Integer resId) {
        try {
            return Result.ok(dynamicPropertyService.excute(resId));
        } catch (Exception ex) {
            logger.error("查询动态调节条件执行结果失败 errmsg={}", ex.getMessage(), ex);
            return Result.error(String.format("查询动态调节条件执行结果失败 [%s]", ex.getMessage()));
        }
    }
}
